<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10">
        <div class="mainor_form">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
            id="form"
          >
            <el-form-item label="活动名称：" prop="title">
              <el-input
                v-model.trim="form.title"
                placeholder="活动名称"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>

            <el-form-item label="活动状态：" prop="status">
              <el-select
                v-model.trim="form.status"
                placeholder="活动状态"
                class="wid220"
                @change="getArticleList"
              >
                <el-option label="所有" :value="parseInt(0)"></el-option>
                <el-option
                  v-for="(item, index) in status_list"
                  :key="index"
                  :label="item"
                  :value="parseInt(index) + 1"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="活动类型：" prop="type">
              <el-select
                v-model.trim="form.type"
                placeholder="活动状态"
                class="wid220"
                @change="getArticleList"
              >
                <el-option label="所有" :value="parseInt(0)"></el-option>
                <el-option
                  v-for="(item, index) in type_list"
                  :key="index"
                  :label="item"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="moredata">
            </MainFormBtn>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-show="moredata">
                <el-form-item label="活动时间：" prop="sendTime">
                  <el-date-picker
                    v-model="sendTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    @change="getArticleList"
                  />
                </el-form-item>
                <div class="clearfix"></div>
                <el-form-item label=" ">
                  <MainFormBtn class="btn-bottom" @reset="reset" @getSearchFor="getArticleList" :loading="listLoading" :showBtn="!moredata">
                  </MainFormBtn>
                </el-form-item>
              </div>
            </el-collapse-transition>
          </el-form>
        </div>
        <div class="main-controls-wrap">
          <div>
            <el-button type="primary" @click="$router.push('/marketing/full/add')">新增活动</el-button>
          </div>
        </div>
        <div class="main_table">
          <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          :header-cell-class-name="this.$verticaline"
          :height="borderHeight"
          :header-cell-style="tableHeightStyle"
          @header-dragend="changeTableWidth"
        >
          <el-table-column
            label="活动名称"
            align="center"
            prop="name"
            :width="tableWidth[hash+'name'] || 120"
          />
          <el-table-column
            label="活动类型"
            align="center"
            prop="type"
            :width="tableWidth[hash+'type'] || 120"
          >
            <template slot-scope="scope">
              <span :class="[scope.row.type == 1 ? 'type1' : 'type2']">
                {{ type_list[scope.row.type] }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="活动时间"
            align="center"
            :width="tableWidth[hash+'active_time'] || 310"
            prop="active_time"
          >
          </el-table-column>
          <el-table-column align="center" label="活动规则" 
          prop="rule_data"
          class-name="main_hei"
          :width="tableWidth[hash+'rule_data'] || 200"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.type == 1">
                <p v-for="(item, index) in scope.row.rule_data" :key="index">
                  满{{ item.price }}元，减{{ item.reduce }}元
                </p>
              </div>
              <div v-if="scope.row.type == 2">
                <p v-for="(item, index) in scope.row.rule_data" :key="index">
                  满{{ item.price }}元，赠送<span
                    v-for="(p, key) in item.goods"
                    :key="key"
                  >
                    {{ p.goods_name }}{{ p.num }}{{ p.spu_name
                    }}{{ key + 1 == item.goods.length ? "" : "、" }}
                  </span>
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="活动状态"
          prop="status"
          :width="tableWidth[hash+'status'] || 120"
          >
            <template slot-scope="scope">
              <span :class="'status' + scope.row.status">
                {{ status_list[scope.row.status] }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="商品限制"
            prop="goods_limit"
            :width="tableWidth[hash+'goods_limit'] || 120"
          >
            <template slot-scope="scope">
              <span :class="'goods_limit' + scope.row.goods_limit">
                {{ goods_limit_type[scope.row.goods_limit] }}
              </span>
              <!-- <span
                  v-for="(item, index) in scope.row.goods_data"
                  :key="index"
                  class="goods_data_span"
                >
                  {{ item.goods_name }}
                </span> -->
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="参与订单"
            prop="order_num"
            :width="tableWidth[hash+'order_num'] || 120"
          />
          <el-table-column align="center" label="创建时间"
          prop="created_at"
          :width="tableWidth[hash+'created_at'] || 200"
          >
            <template slot-scope="scope">
              <span>
                {{ parseTime(scope.row.created_at) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="管理操作"
            fixed="right"
            prop="operate"
            :width="tableWidth[hash+'operate'] || 240"
            class-name="main_hei"
            align="center"
            label-class-name="main-center"
          >
            <template slot-scope="scope">
              <router-link
                :to="
                  '/marketing/full/see/' + scope.row.type + '/' + scope.row.id
                "
              >
                <el-link type="success">查看</el-link>
              </router-link>

              <router-link
                :to="
                  '/marketing/full/edit/' +
                  scope.row.type +
                  '/' +
                  scope.row.id +
                  '?type=copy'
                "
              >
                <el-link type="warning">复制</el-link>
              </router-link>

              <router-link
                :to="
                  '/marketing/full/edit/' + scope.row.type + '/' + scope.row.id
                "
              >
                <el-link
                  v-if="scope.row.status == 2 || scope.row.status == 0"
                  type="info"
                  >编辑</el-link
                >
              </router-link>
              <el-link
                type="danger"
                v-if="scope.row.status != 3"
                @click="handleClose(scope.row)"
                >关闭</el-link
              >
            </template>
          </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import { mapGetters } from "vuex";
import {
  get_full_index,
  get_full_closeActivity,
} from "@/api/marketing/marketing.js";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";

export default {
  mixins: [mixins],
  computed: {
    ...mapGetters(["avatar"]),
  },
  data() {
    return {
      // 时间
      parseTime,
      sendTime: [],
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 更多
      moredata: false,
      // 列表数据
      list: [],
      status_list: [],
      type_list: {},
      listLoading: false,
      // 搜索
      form: {
        start_time: "",
        end_time: "",
        type: null,
        status: null,
        title: "",
      },

      // 全选
      selectArr: [],
      goods_limit_type: ["不限制", "指定商品", "排除商品"],
      moredata: false,
    };
  },

  created() {
    // 列表
    this.getNumerical(200);
    this.get_full_index();
  },
  methods: {
    startGetting() {
      this.getArticleList();
    },
    getSearchFor() {
      this.getArticleList();
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
    },
    // 监听绑定全选不可勾选
    selectInit(row, index) {
      if (row.order_status == 1 && row.order_list_status == 2) {
        return true;
      } else {
        return false;
      }
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.get_full_index();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.sendTime = [];
      this.get_full_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.get_full_index();
    },
    changePage(page) {
      this.current.page = page;
      this.get_full_index();
    },
    // 列表
    get_full_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      get_full_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.data.map((e) => {
            e.new_start_time = "";
            e.new_end_time = "";
            e.new_start_time = this.parseTime(new Date(e.start_time * 1000));
            e.new_end_time = this.parseTime(new Date(e.end_time * 1000));
            e.active_time = e.new_start_time + " 至 " + e.new_end_time;
            e.rule_data = JSON.parse(e.rule_data);
            e.goods_data = JSON.parse(e.goods_data);
          });
          this.totalCount = res.data.list.total;
          this.list = res.data.list.data;
          this.status_list = res.data.status_list;
          this.type_list = res.data.type_list;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.sendTime ? this.sendTime[0] : "";
      form.end_time = this.sendTime ? this.sendTime[1] : "";
      return form;
    },
    // 关闭
    handleClose(row) {
      let id = row.id;
      this.$confirm(
        "活动关闭后无法恢复, 已参与活动的订单不受影响，确认关闭?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(
                  Object.assign(apiUrl().name, this.list_form(), this.current)
                ) + apiUrl().appsecret
              ),
            },
            apiUrl().name,
            {
              id: id,
            }
          );
          get_full_closeActivity(data).then((res) => {
            if (res.errcode == 0) {
              this.$message.success("活动关闭成功");
              this.get_full_index();
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 120px);
  }
  .main_num {
    .mainsu_te {
      position: relative;
      top: -1px;
    }
  }
}
/deep/.el-progress-bar__innerText {
  transform: translateY(-1px);
}
.main_dialog {
  .main_table {
    /deep/.el-table__body-wrapper {
      overflow-y: auto;
      height: 400px;
    }
  }
  .main_xq ul li {
    width: 33.33%;
    margin-top: 5px;
    margin-bottom: 5px;
  }
  .progress_list {
    margin-top: 10px;
    p {
      height: 24px;
      line-height: 24px;
    }
    .progress_list_red {
      color: red;
    }
    .progress_list_su {
      color: #03a98e;
    }
  }
}

.type1 {
  color: #03a98e;
}
.type2 {
  color: #aacd6e;
}

.status0 {
  color: #aacd6e;
}
.status1 {
  color: #03a98e;
}
.status2 {
  color: #f56c6c;
}
.status3 {
  color: #cccccc;
}
/deep/ .main_table .el-table--small td .cell {
  height: auto;
  p {
    margin: 0 5px;
    background-color: #f0f9eb;
    color: #67c23a;
    padding: 2px 0;
  }
  .goods_data_span {
    background-color: #fef0f0;
    color: #f56c6c;
    padding: 2px;
    margin-right: 2px;
    margin-bottom: 5px;
    display: inline-block;
  }
}
.goods_limit0 {
  color: #03a98e;
}
.goods_limit1 {
  color: #f56c6c;
}
.goods_limit2 {
  color: #67c23a;
}
/deep/ .el-form-item--small.el-form-item {
  margin-bottom: 10px !important;
}
</style>
